<template>
  <Modal
    v-model="modal"
    :title="edit ? '新增' : '编辑'"
    @on-cancel="canceledit('modalForm')"
  >
    <Form ref="modalForm" :model="params" :rules="Rules" :label-width="80">
      <FormItem label="名称" prop="name">
        <Input v-model="params.name" placeholder="请输入权限组名称"></Input>
      </FormItem>
      <FormItem label="状态" prop="desc">
        <Switch v-model="params.status" />
      </FormItem>
    </Form>
    <div slot="footer">
      <Button @click="canceledit('modalForm')">取消</Button>
      <Button @click="submitedit('modalForm')" type="primary">确认</Button>
    </div>
  </Modal>
</template>

<script>
import { netsettingaddedit } from "@/api/hqapi";
export default {
  name: "Edit",
  data() {
    return {
      Rules: {
        name: [
          { required: true, message: "请输入权限组名称", trigger: "blur" },
        ],
      },
      modal: false,
      edit: true,
      params: { id: "", name: "", status: true },
    };
  },
  methods: {
    initial(row) {
      if (row) {
        this.params = { id: row.id, name: row.name, status: +row.status === 1 };
        this.edit = false;
      } else {
        this.params = { id: "", name: "", status: true };
        this.edit = true;
      }
      this.modal = true;
    },
    canceledit(name) {
      this.$refs[name].resetFields();
      this.modal = false;
    },
    submitedit(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          let params = {
            id: this.params.id,
            name: this.params.name,
            status: this.params.status ? 1 : 0,
          };
          netsettingaddedit(params).then((res) => {
            this.$Message.success("配置成功");
            this.$emit("askData");
            this.modal = false;
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
